package com.jiamao.goplay.ui.user;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.gyf.immersionbar.ImmersionBar;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.adapter.UserWanToGoAdapter;
import com.jiamao.goplay.app.AppViewModelFactory;
import com.jiamao.goplay.base.BaseMVVMFragment;
import com.jiamao.goplay.databinding.UserWantgotoLayoutBinding;
import com.jiamao.goplay.ui.user.vm.UserWantGotoVM;
import com.jiamao.goplay.view.HorizontalItemDecoration;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;


/**
 * 我想去的
 */
public class UserWantGoToFragment extends BaseMVVMFragment<UserWantgotoLayoutBinding, UserWantGotoVM> {


    private UserWanToGoAdapter adapter;
    private boolean isEdit;
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.user_wantgoto_layout;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public UserWantGotoVM initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(UserWantGotoVM.class);
    }


    @Override
    public void initViewObservable() {
        viewModel.uc.refreshEvent.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(@Nullable Integer aBoolean) {
                binding.refreshLayout.finishRefresh();
                binding.refreshLayout.finishLoadMore();
            }
        });

        viewModel.uc.refreshNUms.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(@Nullable Integer aBoolean) {
                binding.tvNums.setText(aBoolean+"个想去");
               // binding.refreshLayout.finishLoadMore();
            }
        });
        super.initViewObservable();
    }

    @Override
    public void initData() {
        super.initData();

        TextView tv=(TextView)binding.getRoot().findViewById(R.id.title_right);
        tv.setText("编辑");
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isEdit = !isEdit;
                if(isEdit){
                    tv.setText("完成");
                }else {
                    tv.setText("编辑");
                }
                adapter.updateEdit(isEdit);
            }
        });

        binding.refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                viewModel.getDataList(0);
            }
        });
        binding.refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                viewModel.getDataList(1);
            }
        });
        adapter = new UserWanToGoAdapter(getContext());
        binding.setAdapter(adapter);
        ImmersionBar.with(this). keyboardEnable(false).statusBarDarkFont(true).titleBar(binding.getRoot().findViewById(R.id.toolbar)).init();
        TextView titleTv =  binding.getRoot().findViewById(R.id.tvTitle);
        titleTv.setText("我想去的");
        binding.getRoot().findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
       // binding.userGotoListview.addItemDecoration(new HorizontalItemDecoration(9,getContext()));//10表示10dp
     //   binding.userGotoListview.setEmptyView(binding.idEmptyView);
        binding.refreshLayout.autoRefresh();

    }
}
